package com.huihuiyun.huihuiyun.server.controller.manager.website;

import com.huihuiyun.huihuiyun.server.controller.manager.website.authorize.ManagerWebsiteAuthorizeHandler;
import com.huihuiyun.huihuiyun.server.controller.manager.website.viewobjects.HhyWaitingReviewUniteMallProductVO;
import com.huihuiyun.huihuiyun.server.domain.HhyProduct;
import com.huihuiyun.huihuiyun.server.domain.HhyManager;
import com.huihuiyun.huihuiyun.server.service.HhyProductService;
import com.huihuiyun.huihuiyun.server.viewobject.ResultVO;
import com.huihuiyun.huihuiyun.server.viewobject.ResultVOUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.util.List;

@Path(Const.context)
@Component
@Slf4j
public class ManagerWebsiteWaitingReviewUniteMallProductController {
    @Autowired
    private ManagerWebsiteAuthorizeHandler managerWebsiteAuthorizeHandler;

    @Autowired
    private HhyProductService hhyWaitingReviewUniteMallProductService;

    @Context
    private HttpServletRequest httpServletRequest;

    @Context
    private HttpServletResponse httpServletResponse;

    /**
     * 是否有权限
     */
    private static boolean hasRight(HhyManager hhyManager, HhyProduct hhyWaitingReviewUniteMallProduct) {
        return true;
//        return StringUtils.equals(hhyWaitingReviewUniteMallProduct.managerId, hhyManager.id);
    }

    @POST
    @Path("/waitingReviewUniteMallProduct/add")
    @Produces(MediaType.APPLICATION_JSON)
    public ResultVO add(HhyWaitingReviewUniteMallProductVO hhyWaitingReviewUniteMallProductVO) {
        try {
            // 鉴权
            HhyManager hhyManager = managerWebsiteAuthorizeHandler.getHhyManager(httpServletRequest);
            if (hhyManager == null) {
                return managerWebsiteAuthorizeHandler.createNoAuthorizedResultVO(httpServletResponse);
            }
            // 业务
            HhyProduct hhyWaitingReviewUniteMallProduct = ResultVOUtil.convert(hhyWaitingReviewUniteMallProductVO, HhyProduct.class);
            hhyWaitingReviewUniteMallProduct = hhyWaitingReviewUniteMallProductService.put(hhyWaitingReviewUniteMallProduct);
            hhyWaitingReviewUniteMallProductVO = ResultVOUtil.convert(hhyWaitingReviewUniteMallProduct, HhyWaitingReviewUniteMallProductVO.class);
            return ResultVOUtil.success(hhyWaitingReviewUniteMallProductVO);
        } catch (Exception e) {
            log.error("", e);
            return ResultVOUtil.error("创建失败，请稍后重试");
        }
    }

    @POST
    @Path("/waitingReviewUniteMallProduct/deleteOne")
    @Produces(MediaType.APPLICATION_JSON)
    public ResultVO deleteOne(HhyWaitingReviewUniteMallProductVO hhyWaitingReviewUniteMallProductVO) {
        try {
            // 鉴权
            HhyManager hhyManager = managerWebsiteAuthorizeHandler.getHhyManager(httpServletRequest);
            if (hhyManager == null) {
                return managerWebsiteAuthorizeHandler.createNoAuthorizedResultVO(httpServletResponse);
            }
            String id = hhyWaitingReviewUniteMallProductVO.id;
            HhyProduct hhyWaitingReviewUniteMallProduct = hhyWaitingReviewUniteMallProductService.findOne(id);
            if (!hasRight(hhyManager, hhyWaitingReviewUniteMallProduct)) {
                return ResultVOUtil.error("没有权限");
            }
            // 业务
            hhyWaitingReviewUniteMallProductService.delete(id);
            return ResultVOUtil.success();
        } catch (Exception e) {
            log.error("", e);
            return ResultVOUtil.error("删除失败，请稍后重试");
        }
    }

    @POST
    @Path("/waitingReviewUniteMallProduct/deleteAll")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public ResultVO deleteAll(List<HhyWaitingReviewUniteMallProductVO> hhyWaitingReviewUniteMallProductVOList) {
        try {
            // 鉴权
            HhyManager hhyManager = managerWebsiteAuthorizeHandler.getHhyManager(httpServletRequest);
            if (hhyManager == null) {
                return managerWebsiteAuthorizeHandler.createNoAuthorizedResultVO(httpServletResponse);
            }
            for (HhyWaitingReviewUniteMallProductVO hhyWaitingReviewUniteMallProductVO : hhyWaitingReviewUniteMallProductVOList) {
                String id = hhyWaitingReviewUniteMallProductVO.id;
                HhyProduct hhyWaitingReviewUniteMallProduct = hhyWaitingReviewUniteMallProductService.findOne(id);
                if (!hasRight(hhyManager, hhyWaitingReviewUniteMallProduct)) {
                    return ResultVOUtil.error("没有权限");
                }
            }

            List<HhyProduct> hhyWaitingReviewUniteMallProductList = ResultVOUtil.convertList(hhyWaitingReviewUniteMallProductVOList, HhyProduct.class);
            // 业务
            for (HhyProduct hhyWaitingReviewUniteMallProduct : hhyWaitingReviewUniteMallProductList) {
                hhyWaitingReviewUniteMallProductService.delete(hhyWaitingReviewUniteMallProduct.id);
            }
            return ResultVOUtil.success();
        } catch (Exception e) {
            log.error("", e);
            return ResultVOUtil.error("删除失败，请稍后重试");
        }
    }

    @POST
    @Path("/waitingReviewUniteMallProduct/update")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public ResultVO updateWaitingReviewUniteMallProduct(HhyWaitingReviewUniteMallProductVO hhyWaitingReviewUniteMallProductVO) {
        try {
            // 鉴权
            HhyManager hhyManager = managerWebsiteAuthorizeHandler.getHhyManager(httpServletRequest);
            if (hhyManager == null) {
                return managerWebsiteAuthorizeHandler.createNoAuthorizedResultVO(httpServletResponse);
            }
            String id = hhyWaitingReviewUniteMallProductVO.id;
            HhyProduct hhyWaitingReviewUniteMallProduct = hhyWaitingReviewUniteMallProductService.findOne(id);
            if (!hasRight(hhyManager, hhyWaitingReviewUniteMallProduct)) {
                return ResultVOUtil.error("没有权限");
            }
            // 业务
            hhyWaitingReviewUniteMallProduct = ResultVOUtil.copyProperties(hhyWaitingReviewUniteMallProductVO, hhyWaitingReviewUniteMallProduct);
            hhyWaitingReviewUniteMallProduct = hhyWaitingReviewUniteMallProductService.put(hhyWaitingReviewUniteMallProduct);
            hhyWaitingReviewUniteMallProductVO = ResultVOUtil.convert(hhyWaitingReviewUniteMallProduct, HhyWaitingReviewUniteMallProductVO.class);
            return ResultVOUtil.success(hhyWaitingReviewUniteMallProductVO);
        } catch (Exception e) {
            log.error("", e);
            return ResultVOUtil.error("更新失败，请稍后重试");
        }
    }

    @GET
    @Path("/waitingReviewUniteMallProduct/findOne")
    @Produces(MediaType.APPLICATION_JSON)
    public ResultVO<HhyWaitingReviewUniteMallProductVO> findWaitingReviewUniteMallProduct(@QueryParam("id") String id) {
        try {
            // 鉴权
            HhyManager hhyManager = managerWebsiteAuthorizeHandler.getHhyManager(httpServletRequest);
            if (hhyManager == null) {
                return managerWebsiteAuthorizeHandler.createNoAuthorizedResultVO(httpServletResponse);
            }
            HhyProduct hhyWaitingReviewUniteMallProduct = hhyWaitingReviewUniteMallProductService.findOne(id);
            if (!hasRight(hhyManager, hhyWaitingReviewUniteMallProduct)) {
                return ResultVOUtil.error("没有权限");
            }
            // 业务
            HhyWaitingReviewUniteMallProductVO hhyWaitingReviewUniteMallProductVO = ResultVOUtil.convert(hhyWaitingReviewUniteMallProduct, HhyWaitingReviewUniteMallProductVO.class);
            return ResultVOUtil.success(hhyWaitingReviewUniteMallProductVO);
        } catch (Exception e) {
            log.error("", e);
            return ResultVOUtil.error("获取数据失败，请稍后重试");
        }
    }

    @GET
    @Path("/waitingReviewUniteMallProduct/findAll")
    @Produces(MediaType.APPLICATION_JSON)
    public ResultVO findAllWaitingReviewUniteMallProduct(@QueryParam("currentPage") @DefaultValue("1") int currentPage, @QueryParam("pageSize") @DefaultValue("10") int pageSize) {
        try {
            // 鉴权
            HhyManager hhyManager = managerWebsiteAuthorizeHandler.getHhyManager(httpServletRequest);
            if (hhyManager == null) {
                return managerWebsiteAuthorizeHandler.createNoAuthorizedResultVO(httpServletResponse);
            }
            // 业务
            String managerId = hhyManager.id;
            currentPage = ResultVOUtil.getCurrentPageFromAntDesignProQueryParam(currentPage);
            Page<HhyProduct> waitingReviewUniteMallProductPage = hhyWaitingReviewUniteMallProductService.findAll(currentPage, pageSize);
            Object data = ResultVOUtil.convertJpaPage2AntDesignTableData(waitingReviewUniteMallProductPage, HhyWaitingReviewUniteMallProductVO.class);
            return ResultVOUtil.success(data);
        } catch (Exception e) {
            log.error("", e);
            return ResultVOUtil.error("获取数据失败，请稍后重试");
        }
    }

}
